# Sap.tcl - Simulation for ArduPilot
#
# Copyright (C) 2010  Paul Hopkins  email:- hopslink@gmail.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ver. 0.8.3 

#exec tclsh "$0" ${1+"$@"}

package require Tk
package require udp 1.0.8

# hide/minimise the main window (shown when GUI is drawn)
wm withdraw .

# DEBUG - show a console window
#console show

# save executable directory locations 
set _DIR(script)    [file dirname [info script]]
set _DIR(exe)       [file dirname [info nameofexecutable]]

# save library locations
set _DIR(lib)       [file join $_DIR(script) library]
set _DIR(images)    [file join $_DIR(script) images]

# add library location to path (temporary)
lappend auto_path $_DIR(lib)

# Create a namespace for program variables
namespace eval ::sap {
	variable S
	
	set S(sap_version) "0.8.3"	;# version number 
	
	set S(Window)		{}	;# Window handle

	set S(latData) 0	;# values from ArduPilot for display
	set S(lonData) 0
	set S(spdData) 0
	set S(crtData) 0
	set S(altData) 0
	set S(crsData) 0
	set S(berData) 0
	set S(wpnData) 0
	set S(dstData) 0
	set S(btvData) 0
	set S(rsptData) 0
	set S(towData) 0
	set S(towOldData) 0
	
	set S(throttleData) 0	;# control positions from ArduPilot 
	set S(elevatorData) 0
	set S(aileronData) 0
	
	set S(win.commConnectButton) {}		;# handle for comm connect control
	set S(win.commDisconnectButton) {}	;# handle for comm disconnect control
	
	set S(log.FileName) {}	 	;# name for data logging file
	set S(log.Data)	[list]		;# list of all input data
	set S(log.Flag) 1		;# flag indicating datalogging required
	set S(log.KmlDeltaMilliseconds) 1000	;# time delta between points in kml export (milliseconds)
	
	# file type definitions for opening save files
	set S(log.FileTypes)   { { "Binary files" {.log} } { "All files" * } }
	set S(log.FileTypesKml)   [list [list "KML files" {.kml}] [list "All files" * ] ]  
	set S(log.FileTypesCSV) [list [list "CSV files" {.csv}] [list "All files" * ] ]
	
	#Scale factors - named constants not 'magic numbers'
	set S(scaleAPLocationInt2Float) 1000000.0	
}

# add source files
source [file join $_DIR(script) Sap-Win.tcl]
source [file join $_DIR(script) Sap-Comm.tcl]
source [file join $_DIR(script) Sap-Log.tcl]


# every - performs recurring tasks at set intervals
# arguments:
#  ms - required time interval in milliseconds
#  body - a command or function call
#
# eg: every 1000 {set ::sap::S(logFlag) 1}
#
proc every {ms body} {
	eval $body; after $ms [info level 0]	
}
 
# log start time
::sap::log_Startup

# Show GUI
::sap::win 

# And that's it - code is event driven from GUI and comms events
